#include"game.h"

//1.初始化
void Init_board(char arr[rows][cols],int row,int col)
{
		int i = 0;
		int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			arr[i][j] =' ';
		}
	}
}


//2.显示棋盘
void print_board(char arr[rows][cols],int row,int col)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		int j = 0;
		for (j = 0; j < col; j++)
		{
			printf(" %c ", arr[i][j]);
			if(j < col-1)
				printf("|");
		}
			printf("\n");
		if (i < row - 1)
		{
			int j = 0;
			for (j = 0; j < col ; j++)
			{
					printf("---");
				if (j < col - 1)
					printf("|");
			}
			printf("\n");
		}
	}
}


//3.玩家下棋
void Player_action(char arr[rows][cols],int row,int col)
{
	int i = 0;
	int j = 0;
	printf("玩家下棋=<\n");

	while (1)
	{
		printf("请输入坐标=>:");
		scanf("%d %d", &i,&j);
		
		if(i >= 1 && i <= row && j >= 1 && j <= col)
		{
			if(arr[i - 1][j - 1] == ' ')
			{
				arr[i - 1][j - 1] = '*';
				break;
			}
			else
			{
				printf("坐标已被占用，重新输入\n");
			}
		}
		else
		{
			printf("坐标非法，重新输入\n");
		}

	}

}


//4.电脑下棋
void Computer_operation(char arr[rows][cols], int row, int col)
{
		printf("电脑下棋\n");
	int i = 0;
	int y = 0;

	while(1)
	{ 
		i = rand() % row;
		y = rand() % col;
		if (arr[i][y] == ' ')
		{
			arr[i][y] = '#';
			break;
		}

	}

}


//满了返回1，反之返回0
int draw(char arr[rows][cols], int row, int col)
{
	int i = 0;
	int j = 0;
	
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			if (arr[i][j] == ' ')
			{
				return 0;
			}
		}

	}
	return 1;
}

//5.判断输赢
char Winning(char arr[rows][cols], int row, int col)
{
	//判断行
	int i = 0;
	for (i = 0; i < row; i++)
	{
		if (arr[i][0] == arr[i][1] && arr[i][1] == arr[i][2] && arr[i][1] != ' ')
		{
			return arr[i][i];
		}
	}
	//判断列
	int j = 0;
	for (j = 0; j < col; j++)
	{
		if (arr[0][j] == arr[1][j] && arr[1][j] == arr[2][j] && arr[1][j] != ' ')
		{
			return arr[1][j];
		}
	}
	//判断对角线
	if (arr[0][0] == arr[1][1] && arr[1][1] == arr[2][2] && arr[1][1] != ' ')
	{
		return arr[1][1];
	}

	if (arr[0][2] == arr[1][1] && arr[1][1] == arr[2][0] && arr[1][1] != ' ')
	{
		return arr[1][1];
	}
	//判断平局
	if (draw(arr, row, col))
	{
		return 'C';
	}
	return 'F';
}